﻿using System;
using System.Security.Cryptography;
using System.Web;
using tour.szhome.common;

namespace TOUR.SZHOME.Attributes
{
    /// <summary>
    /// QiBu授权
    /// </summary>
    public class Authentication
    {
        public static string AUTH_COOKIE_NAME = "TOUR_Authentication";

        /// <summary>
        /// 设置用户COOKIE
        /// </summary>
        /// <param name="userInfo">用户信息</param>
        /// <param name="createPersistentCookie">是否持久化</param>
        public static void SetAuthCookie(string userInfo, bool createPersistentCookie = false)
        {
            string skey = MakeServerKey();
            //HttpContext.Current.Response.Headers.Add("P3P: CP", "CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR");
            var _authTicket = new AuthenticationTicket(userInfo, DateTime.Now, DateTime.Now.AddDays(15), createPersistentCookie, skey);
            var _jsonD = MD5AndXOREncrypt._Encrypt(_authTicket.ToJson());
            var _authCookie = new HttpCookie(AUTH_COOKIE_NAME, _jsonD);
            _authCookie.Domain = "t.szhome.com";
            if (createPersistentCookie)
            {
                _authCookie.Expires = DateTime.Now.AddDays(15);
            }
            HttpContext.Current.Response.Cookies.Add(_authCookie);
        }

        //获取用户信息
        public static AuthenticationTicket GetUserInfo()
        {
            string enCryptTicketInfo = HttpContext.Current.Request.Cookies[AUTH_COOKIE_NAME].Value;
            string ticketInfo = MD5AndXOREncrypt.Decrypt(enCryptTicketInfo);
            var jsonHelper = new System.Web.Script.Serialization.JavaScriptSerializer();
            return jsonHelper.Deserialize<AuthenticationTicket>(ticketInfo);
        }

        //注销
        public static void SignOut()
        {
            var authCookie = HttpContext.Current.Response.Cookies[AUTH_COOKIE_NAME];
            if (authCookie != null)
            {
                authCookie.Expires = DateTime.Now.AddDays(-365);
                authCookie.Domain = "t.szhome.com";
            }
        }

        //产生随机Key
        private static string MakeServerKey()
        {
            //要使用生成KEY的字符
            string[] chars = new string[]{
                "a","b","c","d","e","f","g","h",
                "i","j","k","l","m","n","o","p",
                "q","r","s","t","u","v","w","x",
                "y","z","0","1","2","3","4","5",
                "6","7","8","9","A","B","C","D",
                "E","F","G","H","I","J","K","L",
                "M","N","O","P","Q","R","S","T",
                "U","V","W","X","Y","Z"
              };

            Random ra = new Random(unchecked((int)DateTime.Now.Ticks));
            int num = 8;
            string[] arrNum = new string[num];
            int tmp = 0;
            for (int i = 0; i <= num - 1; i++)
            {
                tmp = ra.Next(0, 61); //随机取数
                arrNum[i] = chars[tmp];
            }
            return "@" + string.Concat(arrNum);
        }
    }
}
